---
sidebar_position: 5
---

# Item Guide

The `Item` class is used to handle all item-related operations on the server. Below, the main functions and usage of the `Item` class will be described.

## `Item` class overview \{#item-class-overview}

Located in the [cn.nukkit.item](https://github.com/MemoriesOfTime/Nukkit-MOT/blob/master/src/main/java/cn/nukkit/item/Item.java) package.

Provides a large number of methods for creating and modifying items in the game, which can be blocks, tools, food, etc.

## Constructors \{#constructors}

The `Item` class provides several constructors that allow the creation of item instances from different parameters.

### Example code: \{#example-code}
```java
// Import the Item class
import cn.nukkit.item.Item;

// Create the item by item ID and quantity
Item diamond = Item.get(Item.DIAMOND, 0, 1); // create 1 diamond
// Give the player a diamond
player.giveItem(diamond);
// Or
player.getInventory().addItem(diamond);

// Create items via namespaces (recommended!)
Item apple = Item.fromString("minecraft:apple");
```

### Code explanation: \{#code-explanation}

:::warning

Always use the `Item.get()` or `Item.fromString()` method to get an item, rather than instantiating the item object `new ItemDiamond(id, meta, count)`!

:::

- `Item.DIAMOND`: This is the item ID of the diamond, each item has a unique ID.
- `0`: This is the item's variant number, for most items this will be 0.
- `1`: This represents the number of the item.

## Main Methods \{#main-methods}

Below are some commonly used methods and examples in the `Item` class.

### `getId()` \{#get-id}
Returns the ID of the item.
```java
int id = diamond.getId(); // Get the ID of the diamond
// Output: 264
```

### `getNamespaceId()` \{#get-namespace-id}
Returns the namespace of the item.
```java
String namespace = diamond.getNamespaceId(); // Get the namespace of the diamond
// Output: minecraft:diamond
```

### `getCount()` \{#get-count}
Returns the quantity of the item.
```java
int count = diamond.getCount(); // Get the quantity of the diamond
// Output: 1
```

### `getName()` \{#get-name}
Returns the name of the item.
```java
String name = diamond.getName(); // Get the name of the diamond
// Output: Diamond
```

### `getEnchantmentLevel(int id)` \{#get-enchantment-level}
Get the enchantment level of the item.
You can find relevant enchantment IDs in [cn.nukkit.item.enchantment.Enchantment](https://github.com/MemoriesOfTime/Nukkit-MOT/blob/master/src/main/java/cn/nukkit/item/enchantment/Enchantment.java).
```java
int level = diamond.getEnchantmentLevel(Enchantment.ID_DAMAGE_ALL); // Get the diamond's sharpness level. If it's not enchanted, return 0.
// Output: 0
```

## Handling item properties \{#handling-item-properties}

Attributes of items can be managed through various setter and getter methods.

### Setting the number of items \{#set-number-of-items}
```java
diamond.setCount(3); // Set the number of diamonds to 3
```

### Example Code \{#example-code-properties}

1. Setting Basic Item Properties

```java
Item diamond = Item.get(Item.DIAMOND, 0, 1);
// You can continue to add more lines
diamond.setLore("Line 1", "Line 2");
// If you do not want the text to be italicized, you can add "§r" at the beginning of the text to reset the format
diamond.setCustomName("Custom Name of the Item");
// Add Sharpness 5 and Protection 4 enchantments to the item
diamond.addEnchantment(Enchantment.get(Enchantment.ID_DAMAGE_ALL).setLevel(5), Enchantment.get(Enchantment.ID_PROTECTION_ALL).setLevel(4));
```

2. Infinite Durability for Items

```java
// Achieved through nbt
CompoundTag tag = diamond.hasCompoundTag() ? diamond.getNamedTag() : new CompoundTag();
tag.putByte("Unbreakable", 1);
// After modifying nbt, you need to setNamedTag for it to take effect
diamond.setNamedTag(tag);
```

3. Item Lock Modes

Nukkit-MOT provides the `Item.ItemLockMode` class, and developers can use `setItemLockMode(ItemLockMode lockMode)` to set the lock mode of an item.

- `Item.ItemLockMode.LOCK_NONE`

Remove item lock

- `Item.ItemLockMode.LOCK_IN_INVENTORY`

![lock in inventory](https://minecraft.wiki/images/Item_lock_yellow.png) the texture used over the item.

Prevents the item from being removed from the player's inventory, dropped, or crafted with.

- `Item.ItemLockMode.LOCK_IN_SLOT`

![lock in slot](https://minecraft.wiki/images/Item_lock_red.png) the texture used over the item.

Prevents the item from being moved or removed from its slot in the player's inventory, dropped, and by extension, crafted with.

:::note

Supported since 1.16.100 (protocol version 419)

:::

```java
diamond.setItemLockMode(Item.ItemLockMode.LOCK_IN_INVENTORY);
```